package action.newelifile;

import action.adapter.SearchLocalFileAdapter;
import action.pojo.LocalFilePojo;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ListSearchActivity extends Activity{
	ListView list_search;
	SearchLocalFileAdapter fileAdapter;
	TextView searchresult;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		setContentView(R.layout.list_search);	
		getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
		searchresult = (TextView)findViewById(R.id.tvHeaderSearch);
		searchresult.setText(R.string.searchresult);
		list_search = (ListView)findViewById(R.id.list_search);
		fileAdapter = new SearchLocalFileAdapter(ListSearchActivity.this, R.layout.list_detail,SearchActivity.array);
		list_search.setAdapter(fileAdapter);
		list_search.setOnItemClickListener(onItemClick());
		((Button)findViewById(R.id.cancel)).setOnClickListener(cancel());

		super.onCreate(savedInstanceState);
	}
	
	public OnItemClickListener onItemClick(){
		OnItemClickListener clickListener = new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				LocalFilePojo object = (LocalFilePojo) parent.getItemAtPosition(position);
				if (object.getFileItem().isDirectory()) {
					Intent intent = new Intent(ListSearchActivity.this,LocalActivity.class);
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					intent.putExtra("isSearch", true);
					intent.putExtra("src", object.getFileItem().getPath());
					startActivity(intent);
				} else {
					Intent intent = object.getAction();
					startActivity(intent);
				}
			}
		};
		return clickListener;
	}
	
	private OnClickListener cancel(){
		OnClickListener action = new OnClickListener() {
			
			public void onClick(View v) {
				finish();
			}
		};
		return action;
	}
}
